package com.woniu.after.service.impl;

import java.math.BigDecimal;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.base.Joiner;
import com.woniu.after.common.RefundsApplyException;
import com.woniu.after.common.RefundsException;
import com.woniu.after.common.ReturnException;
import com.woniu.after.infra.model.Refunds;
import com.woniu.after.infra.model.RefundsApply;
import com.woniu.after.infra.model.Return;
import com.woniu.after.infra.mapper.ReturnMapper;
import com.woniu.after.infra.model.SalesReturnProduct;
import com.woniu.after.service.RefundsApplyService;
import com.woniu.after.service.RefundsService;
import com.woniu.after.service.ReturnService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.after.service.SalesReturnProductService;
import com.woniu.after.service.dto.ReturnDto;
import com.woniu.after.service.form.ReturnGoodsForm;
import com.woniu.exception.LXLGException;
import com.woniu.order.client.OrderEntryQueryClient;
import com.woniu.order.client.param.SearchOrderEntryParam;
import com.woniu.order.client.result.OrderEntryQueryResultInfo;
import com.woniu.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 退款退货表 服务实现类
 * </p>
 *
 * @author jianlang
 * @since 2022-11-26
 */
@Service
public class ReturnServiceImpl extends ServiceImpl<ReturnMapper, Return> implements ReturnService {

    @Resource
    private Snowflake snowflake;

    @Resource
    private RefundsApplyService refundsApplyService;

    @Resource
    private RefundsService refundsService;

    @Resource
    private OrderEntryQueryClient orderEntryQueryClient;

    @Resource
    private SalesReturnProductService salesReturnProductService;

    @Override
    public ReturnDto userReturnGoods(ReturnGoodsForm returnGoodsForm) {
        Return aReturn = BeanUtil.copyProperties(returnGoodsForm, Return.class);
        aReturn.setReturnsNum("LXLG" + snowflake.nextId());
        RefundsApply refundsApply = refundsApplyService.getById(returnGoodsForm.getRefundsApplyId());
        if (refundsApply == null) throw new LXLGException(RefundsApplyException.REFUNDS_APPLY_NON_EXISTENT);
        String[] split = refundsApply.getProductId().split(",");
        aReturn.setOrderId(refundsApply.getOrderId());
        aReturn.setOrderlogisticsStatus(1);
        aReturn.setReturnMoney(refundsApply.getBackMoney());
        aReturn.setHandlingTime(DateUtil.parseDateLong());
        aReturn.setReturnRemarks(refundsApply.getRemarks());
        Refunds refunds = refundsService.getOne(new QueryWrapper<Refunds>().eq("refunds_apply_id", refundsApply.getId()));
        if (refunds == null) throw new LXLGException(RefundsException.Refunds_NON_EXISTENT);
        aReturn.setRefundId(refunds.getId());
        List<Integer> salesReturnProductId = returnGoodsForm.getSalesReturnProductId();
        //对ids排序
        Collections.sort(salesReturnProductId);
        //用,连接
        String ids = Joiner.on(',').join(salesReturnProductId);
        aReturn.setSalesReturnProductId(ids);
        aReturn.setRefundsApplyId(refundsApply.getId());


        if (returnGoodsForm.getSalesReturnProductId() != null && returnGoodsForm.getSalesReturnProductId().size() > 0) {
            if (returnGoodsForm.getSalesReturnProductId().size() != split.length)
                throw new LXLGException(ReturnException.RETURN_EXCEPTION);

            aReturn.setReturnsType(2);

            for (Integer integer : returnGoodsForm.getSalesReturnProductId()) {
                for (String s : split) {

                }
                SalesReturnProduct salesReturnProduct = new SalesReturnProduct();
                salesReturnProduct.setRefundsId("TH" + snowflake.nextId());
                salesReturnProduct.setProductId(integer);
                salesReturnProduct.setOrderId(refundsApply.getOrderId());

                SearchOrderEntryParam searchOrderEntryParam = new SearchOrderEntryParam();
                searchOrderEntryParam.setOrderId(refundsApply.getOrderId());
                searchOrderEntryParam.setSkuId(integer);

                OrderEntryQueryResultInfo data = orderEntryQueryClient.searchOrderEntry(searchOrderEntryParam).getData();

                salesReturnProduct.setRefundsMoney(data.getPrice().multiply(BigDecimal.valueOf(data.getQuantity())));
                salesReturnProduct.setReturnCount(data.getQuantity());

                salesReturnProduct.setRefundsTime(refunds.getRefundTime());
                salesReturnProduct.setReturnTime(DateUtil.parseDateLong());
                salesReturnProduct.setStatus("4");
                salesReturnProductService.save(salesReturnProduct);
            }
        } else {
            aReturn.setReturnsType(1);
            List<OrderEntryQueryResultInfo> data = orderEntryQueryClient.searchOrderAllEntry(refundsApply.getOrderId()).getData();
            for (OrderEntryQueryResultInfo datum : data) {
                SalesReturnProduct salesReturnProduct = new SalesReturnProduct();
                salesReturnProduct.setRefundsId("TH" + snowflake.nextId());
                salesReturnProduct.setProductId(datum.getSkuId());
                salesReturnProduct.setOrderId(datum.getOrderId());
                salesReturnProduct.setRefundsMoney(datum.getPrice().multiply(BigDecimal.valueOf(datum.getQuantity())));
                salesReturnProduct.setReturnCount(datum.getQuantity());
                salesReturnProduct.setRefundsTime(refunds.getRefundTime());
                salesReturnProduct.setReturnTime(DateUtil.parseDateLong());
                salesReturnProduct.setStatus("4");
                salesReturnProductService.save(salesReturnProduct);
            }
        }
        save(aReturn);
        return BeanUtil.copyProperties(aReturn, ReturnDto.class);
    }
}
